package lc.q300_350;

import java.util.*;

public class Q347 {

    public int[] topKFrequent(int[] nums, int k) {
        Map<Integer, Integer> counts = new HashMap<>();
        for (int num : nums) {
            counts.put(num, counts.getOrDefault(num, 0) + 1);
        }
        // 优先队列
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(Comparator.comparing(counts::get).reversed());
        for (Map.Entry<Integer, Integer> integerIntegerEntry : counts.entrySet()) {
            priorityQueue.add(integerIntegerEntry.getKey());
        }
        // 排序
        int[] result = new int[k];
        for (int i = 0; i < k; i++) {
            Integer poll = priorityQueue.poll();
            result[i] = poll;
        }
        return result;
    }

    public static void main(String[] args) {
        Q347 q347 = new Q347();
        System.out.println(Arrays.toString(q347.topKFrequent(new int[]{
                1,1,1,2,2,3,3,3,3,4
        }, 2)));
    }
}
